﻿//###HEADER###
//Title: RevitPlus
//Description of Program: Utilzing Revit geometry and properties, user is able to perform an EnergyPlus calculation within Revit.
//Class:BuildingForm
//Description of Class:  User interface within Revit. Contains two catches in case user does not provide a weather file or if a field is left blank.
//       
//By: Steve Chick & Alexander Bernstein
//Date: 12/18/2012
//Version: 001

using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using Autodesk.Revit.DB;
using Autodesk.Revit.UI;

namespace Revit_Addin1
{
    //Define Class
    public partial class BuildingForm : System.Windows.Forms.Form
    {
        //Initialize Form
        public BuildingForm()
        {
            InitializeComponent();
        }

        //Form values when loaded
        private void Form1_Load(object sender, EventArgs e)
        {
            //Set values to StoredVariables on load
            this.EPlusVersion.Items.AddRange(StoredVariables.EPlusVersions.ToArray());
            this.EPlusVersion.SelectedIndex = 0;
            this.NameBox.Text = StoredVariables.ProjectName;
            this.RotationBox.Text = StoredVariables.NorthAxis;
            this.TerrainCombo.SelectedIndex = this.TerrainCombo.Items.IndexOf(StoredVariables.Terrain);
            this.LoadsBox.Text = StoredVariables.LoadTolerance;
            this.SolarCombo.SelectedIndex =this.SolarCombo.Items.IndexOf(StoredVariables.SolarDistribution);
            this.WarmupMaxBox.Text = StoredVariables.WarmpupMax;
            this.WarmupMinBox.Text = StoredVariables.WarmpupMin;

            //Set weather button text to value if the value has been defined previously
            if (StoredVariables.WeatherFile != null)
            {
                this.Weather.Text = StoredVariables.WeatherFile;
            }
            
            this.StMonth.Value = Convert.ToInt32(StoredVariables.StMonth);
            this.StDay.Value = Convert.ToInt32(StoredVariables.StDay);
            this.EndMonth.Value = Convert.ToInt32(StoredVariables.EndMonth);
            this.EndDay.Value = Convert.ToInt32(StoredVariables.EndDay);
            this.Output.SelectedIndex = this.Output.Items.IndexOf(StoredVariables.Time);
        }

        private void OKbutton_Click(object sender, EventArgs e)
        {
            BuildingForm form = (sender as System.Windows.Forms.Control).Parent as BuildingForm;
            form.DialogResult = DialogResult.OK;

            //Set values in StoredVariables class to form inputs
            StoredVariables.EplusVersion = this.EPlusVersion.SelectedText;
            StoredVariables.ProjectName = this.NameBox.Text.ToString();
            StoredVariables.NorthAxis = this.RotationBox.Text.ToString();            
            StoredVariables.LoadTolerance = this.LoadsBox.Text.ToString();            
            StoredVariables.WarmpupMax = this.WarmupMaxBox.Text.ToString();
            StoredVariables.WarmpupMin = this.WarmupMinBox.Text.ToString();
            StoredVariables.StMonth = this.StMonth.Text.ToString();
            StoredVariables.StDay = this.StDay.Text.ToString();
            StoredVariables.EndMonth = this.EndMonth.Text.ToString();
            StoredVariables.EndDay = this.EndDay.Text.ToString();

            //Check to ensure all inputs have a value
            if (StoredVariables.ProjectName != "" & StoredVariables.NorthAxis != "" & StoredVariables.LoadTolerance != "" 
                & StoredVariables.TempTolerance != "" & StoredVariables.WarmpupMax != "" & StoredVariables.WarmpupMin != "" 
                & StoredVariables.CoolSetPt != "" & StoredVariables.HeatSetPt != "" & StoredVariables.StMonth != "" 
                & StoredVariables.StDay != "" & StoredVariables.EndMonth != "" & StoredVariables.EndDay != "")
            {
                //Check to ensure a weather file has been set
                if (StoredVariables.WeatherFile != null)
                {
                    form.Close();
                    MaterialProperties matprop = new MaterialProperties();
                    matprop.Show();
                }
                //Alert the user if no weather file is set
                else
                {
                    TaskDialog.Show("RevitPlus: Input Error", "Please Select A Weather File");
                }
            }
            //Alert the user if inputs are missing
            else
            {
                TaskDialog.Show("RevitPlus: Input Error", "Please Ensure All Input Values Have Been Entered", TaskDialogCommonButtons.Ok);
            }
        }

        //Close the form on cancel
        private void Cancelbutton_Click(object sender, EventArgs e)
        {
            BuildingForm form = (sender as System.Windows.Forms.Control).Parent as BuildingForm;
            form.DialogResult = DialogResult.Cancel;
            form.Close();
        }

        private void Weather_Click(object sender, EventArgs e)
        {
            //Set StoredVariable when a weather file is chosen
            OpenFileDialog weatherfile = new OpenFileDialog();
            weatherfile.Filter = "Weather Files (*.epw)|*.epw";
            weatherfile.Title = "Please Choose A Weather File";
            weatherfile.ShowDialog();
            StoredVariables.WeatherFile = weatherfile.FileName;

            //Alter the button text when a weather file is chosen
            if (StoredVariables.WeatherFile != null)
            {
                this.Weather.Text = StoredVariables.WeatherFile;
            }
        }

        //Set Terrain value when combo box selected index is changed
        private void TerrainCombo_SelectedIndexChanged(object sender, EventArgs e)
        {
            StoredVariables.Terrain = this.TerrainCombo.Items[this.TerrainCombo.SelectedIndex].ToString();
        }

        //Set Solar value when combo box selected index is changed
        private void SolarCombo_SelectedIndexChanged(object sender, EventArgs e)
        {
            StoredVariables.SolarDistribution = this.SolarCombo.Items[this.SolarCombo.SelectedIndex].ToString();
        }

        //Set Output value when combo box selected index is changed
        private void Output_SelectedIndexChanged(object sender, EventArgs e)
        {
            StoredVariables.Time = this.Output.Items[this.Output.SelectedIndex].ToString();
        }
    }
}